<template>
  <div>
    <el-form :model="ruleForm" ref="ruleForm" :label-position="labelPosition"
             :class="isEdit?'':'formItem'">
      <el-card class="box-card" shadow="never">
        <div slot="header" class="header-card">
          <span class="header-card-title">车辆信息</span>
        </div>

        <el-row :gutter="20">
          <el-col :span="8">
            <el-form-item label="车牌号:" prop="carNo" :rules="isEdit ? rules.carNo :[]">
              <el-input v-model="ruleForm.carNo" @focus="handleFocus" placeholder="请选择处置车辆"
                        readonly
                        v-if="isEdit"/>
              <span v-else>{{ ruleForm.carNo }}</span>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="车架号:" prop="vinNo">
              <el-input disabled v-model="ruleForm.vinNo" v-if="isEdit" placeholder="请选择车辆后自动带出"/>
              <span v-else>{{ ruleForm.vinNo }}</span>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="品牌:" prop="brandName">
              <el-input v-model="ruleForm.brandName" disabled v-if="isEdit" placeholder="请选择车辆后自动带出"/>
              <span v-else>{{ ruleForm.brandName }}</span>
            </el-form-item>
          </el-col>

          <el-col :span="8">
            <el-form-item label="车系:" prop="seriesName">
              <el-input v-model="ruleForm.seriesName" disabled v-if="isEdit" placeholder="请选择车辆后自动带出"/>
              <span v-else>{{ ruleForm.seriesName }}</span>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="车型:" prop="modelName">
              <el-input v-model="ruleForm.modelName" disabled v-if="isEdit" placeholder="请选择车辆后自动带出"/>
              <span v-else>{{ ruleForm.modelName }}</span>
            </el-form-item>
          </el-col>


          <el-col :span="8">
            <el-form-item label="车辆产权城市:" prop="propertyRightName">
              <el-input disabled v-model="ruleForm.propertyRightName" v-if="isEdit"
                        placeholder="请选择车辆后自动带出"/>
              <span v-else>{{ ruleForm.propertyRightName }}</span>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="车辆运营城市:" prop="operationsName">
              <el-input disabled v-model="ruleForm.operationsName" v-if="isEdit"
                        placeholder="请选择车辆后自动带出"/>
              <span v-else>{{ ruleForm.operationsName }}</span>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="车辆归属城市:" prop="ascription">
              <el-input disabled v-model="ruleForm.ascription" v-if="isEdit" placeholder="请选择车辆后自动带出"/>
              <span v-else>{{ ruleForm.ascription }}</span>
            </el-form-item>
          </el-col>

          <el-col :span="8">
            <el-form-item label="事故还车时间:" prop="returnTime">
              <el-input disabled v-model="ruleForm.returnTime" v-if="isEdit"
                        placeholder="请选择车辆后自动带出"/>
              <span v-else>{{ ruleForm.returnTime }}</span>
            </el-form-item>
          </el-col>
          <template v-if="ruleForm.claimsMethod == 1">
            <el-col :span="8">
              <el-form-item label="客户理赔款:" prop="compensationClaims">
                <el-input disabled v-model="ruleForm.compensationClaims" v-if="isEdit"
                          placeholder="请选择车辆后自动带出"/>
                <span v-else>{{ ruleForm.compensationClaims }}</span>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="财务收款时间:" prop="financialPassedTime">
                <el-input disabled v-model="ruleForm.financialPassedTime" v-if="isEdit"
                          placeholder="请选择车辆后自动带出"/>
                <span v-else>{{ ruleForm.financialPassedTime }}</span>
              </el-form-item>
            </el-col>
          </template>

          <template v-else>
            <el-col :span="8">
              <el-form-item label="保险理赔款:" prop="compensationClaims">
                <el-input disabled v-model="ruleForm.compensationClaims" v-if="isEdit"
                          placeholder="请选择车辆后自动带出"/>
                <span v-else>{{ ruleForm.compensationClaims }}</span>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="关联维修单:" prop="maintenanceNo">
                <el-input disabled v-model="ruleForm.maintenanceNo" v-if="isEdit" placeholder="请选择车辆后自动带出"/>
                <el-button type="text" @click="handleMaintenanceNo(ruleForm.maintenanceNo)" v-else>{{ ruleForm.maintenanceNo }}</el-button>
              </el-form-item>
            </el-col>

            <el-col :span="8">
              <el-form-item label="报废审批通过时间:" prop="scrapPassedTime">
                <el-date-picker disabled v-model="ruleForm.scrapPassedTime" type="date" v-if="isEdit"
                                placeholder="请选择车辆后自动带出" value-format='yyyy-MM-dd' style="width: 100%;"/>
                <span v-else>{{ ruleForm.scrapPassedTime }}</span>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="财务审批通过时间:" prop="financialPassedTime">
                <el-date-picker disabled v-model="ruleForm.financialPassedTime" type="date" v-if="isEdit"
                                placeholder="请选择车辆后自动带出" value-format='yyyy-MM-dd' style="width: 100%;"/>
                <span v-else>{{ ruleForm.financialPassedTime }}</span>
              </el-form-item>
            </el-col>
          </template>


        </el-row>


      </el-card>

      <el-card class="box-card" shadow="never">
        <div slot="header" class="header-card">
          <span class="header-card-title">订单信息</span>
        </div>

        <el-row :gutter="20">
          <el-col :span="8">
            <el-form-item label="关联订单:" prop="orderNo">
              <el-input disabled v-model="ruleForm.orderNo" placeholder="请选择车辆后自动带出"
                        v-if="isEdit"/>
              <el-button type="text" v-else @click="handleTo(ruleForm.orderNo)">{{ ruleForm.orderNo }}</el-button>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="产品方案:" prop="schemeName">
              <el-input disabled v-model="ruleForm.schemeName" v-if="isEdit" placeholder="请选择车辆后自动带出"/>
              <span v-else>{{ ruleForm.schemeName }}</span>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="客户名称:" prop="customerName">
              <el-input disabled v-model="ruleForm.customerName" v-if="isEdit" placeholder="请选择车辆后自动带出"/>
              <span v-else>{{ ruleForm.customerName }}</span>
            </el-form-item>
          </el-col>
        </el-row>


      </el-card>
    </el-form>
    <car-table ref="refCarTable" @handleSelect="handleSelect"/>
  </div>
</template>

<script>
import * as outInventoryApi from '@/api/carInventory/outInventory'

export default {
  name: 'vehicleMessage',//车辆信息  订单信息
  dicts: ['disposal_status_type'],
  props: {
    value: {
      type: Object,
      default: () => {
      }
    },
    state: {
      type: String,
      default: "",
    },
    status: {
      type: Number,
      default: 24,
    }
  },
  components: {
    carTable: () => import('./carTable.vue')
  },
  computed: {
    ruleForm: {
      get() {
        let that = this;
        const proxy = new Proxy(that.value, {
          get(target, key) {
            return Reflect.get(target, key);
          },
          set(target, key, value) {
            that.$emit("input", {...target, [key]: value});
            return true;
          },
        });
        return proxy;
      },
    },
    labelPosition() {
      if (this.state === 'write') {
        return 'top'
      } else {
        return 'right'
      }
    },
    isEdit() {
      let bool;
      switch (this.state) {
        case "write":
          bool = true;
          break;
        case "disabled":
          bool = false;
          break;
        case "readonly":
          bool = false;
          break;
      }
      return bool
    }
  },
  watch: {
    'value.carNo': {
      handler(val) {
        if (val) {
          this.handleClearValidate('carNo')
        }
      },
      immediate: true,
      deep: true
    }
  },
  data() {
    return {
      rules: {
        carNo: [
          {required: true, message: '请选择车牌号', trigger: 'change'}
        ]
      }
    };
  },
  created() {
  },
  mounted() {
  },
  methods: {
    handleMaintenanceNo(val){

    },

    handleFocus() {
      this.$refs.refCarTable.handleOpen()
    },

    handleSelect(val) {
      if (!val) return;
      for (let key in this.ruleForm) {
        this.$set(this.value, key, val[key] ? val[key] : '');
      }
    },

    handleEffect() {
      return new Promise((resolve, reject) => {
        this.$refs.ruleForm.validate((valid) => {
          if (valid) {
            resolve();
          } else {
            this.locateToErr()
            reject();
          }
        });
      });
    },

    locateToErr() {
      this.$nextTick(() => {
        let isError = document.getElementsByClassName('is-error')
        isError[0].scrollIntoView({
          // 滚动到指定节点
          // 值有start,center,end，nearest，当前显示在视图区域中间
          block: 'center',
          // 值有auto、instant,smooth，缓动动画（当前是慢速的）
          behavior: 'smooth',
        })
      })
    },

    //取消表单效验
    handleClearValidate(str) {
      this.$refs?.ruleForm?.clearValidate(str);
    },

    async handleTo(val) {
      if (!val) return
      try {
        let res = await outInventoryApi.outOrderDetails(val)
        if (res.code === 200) {
          this.$router.push({
            name: 'SuborderDetail',
            query: {
              id: res.data.orderCarId,
              orderId: res.data.orderCode,
            },
          })
        }
      } catch (e) {
        console.error(e)
      }
    }
  },
};
</script>

<style lang="scss" scoped>
@import "@/styles/cwgd/formScss.scss";

.div-status {
  width: 100px;
  height: 100px;
  display: flex;
  justify-content: center;
  align-items: center;
  border-radius: 50%;
  border: 1px solid rgb(64, 158, 255);;
}

el-row {
  display: flex;
  flex-wrap: wrap;
}

::v-deep span:empty::before {
  content: '-';
}
</style>


